import 'package:flutter/material.dart';

class CustomAppBar extends StatelessWidget implements PreferredSizeWidget {
  final Widget? left;
  final Widget? right;
  final Widget? title;
  final double height;
  final bool showBottomLine;
  final Color? backgroundColor;
  final double elevation;
  final bool centerTitle;

  const CustomAppBar({
    super.key,
    this.left,
    this.right,
    this.title,
    this.height = 40,
    this.showBottomLine = true,
    this.backgroundColor,
    this.elevation = 0,
    this.centerTitle = true,
  });

  @override
  Size get preferredSize => Size.fromHeight(height + (showBottomLine ? 1 : 0));

  @override
  Widget build(BuildContext context) {
    return PreferredSize(
      preferredSize: preferredSize,
      child: AppBar(
        elevation: elevation,
        backgroundColor: backgroundColor,
        centerTitle: centerTitle,
        title: title,
        leading: left != null
            ? Padding(
                padding: const EdgeInsets.only(left: 12), // 自定义左边距
                child: left,
              )
            : null,
        actions: right != null
            ? [
                Padding(
                  padding: const EdgeInsets.only(right: 12), // 自定义右边距
                  child: right,
                )
              ]
            : null,
        bottom: showBottomLine
            ? PreferredSize(
                preferredSize: const Size.fromHeight(1),
                child: Container(
                  height: 1,
                  color: Colors.grey.withAlpha(26), // 约0.1透明度
                ),
              )
            : null,
      ),
    );
  }
}
